// 多播运算

import { connect, interval, map, publish, share, take, tap } from "rxjs";


// Subject 是必须要会的，且非常重要
export function subjectDemo() {
  //

}

export function shareDemo() {
  const source = interval(1000).pipe(
    tap((x) => console.log("Processing: ", x)),
    map((x) => x * x),
    take(6),
    share(), // 这个可以当前obserable 支持多个订阅, 这里面还支持一些参数设置： https://rxjs.dev/api/index/interface/ShareConfig
  );

  source.subscribe((x) => console.log("subscription 1: ", x));
  source.subscribe((x) => console.log("subscription 2: ", x));
}

//我理解这玩意， 主要用来处理，多个组件之前的通信问题
export function publishDemo() {
  const rootObs = interval(500).pipe(take(9));

  // 触发的时机交给了 connect 来处理了；里面是个回调函数；统一由这玩意触发；
  const publishObs = rootObs.pipe(connect((x) => x));

  const sub1 = publishObs.subscribe((x) => {
    console.log("111111", x);
  });

  const sub2 = publishObs.subscribe((x) => {
    console.log("2222222", x);
  });

  setTimeout(() => {
    sub1.unsubscribe();
  }, 2000);

  setTimeout(function () {
    sub2.unsubscribe();
  }, 4000);

  // publishObs.conntion();
}
